#
# Hill, Seth J. and Chris Tausanovitch. "A Disconnect in Representation? Comparison of Trends in Congressional and Public Polarization."
#

library(rjags)
library(MCMCpack)

load("allyearsMultinomialFull.RData")

adapt <- 500
iterations <- 2000
thin <- 1
  
starting=list(x=startvalues)

unrolly <- function(data) {
   m <- dim(data$y)[2]; n <- dim(data$y)[1]
   yy <- na.omit(cbind(as.vector(data$y),expand.grid(1:n,1:m)))
   list(yy=yy[,1],nidx=yy[,2],midx=yy[,3],
        lengthy=dim(yy)[1],n=n,m=m,y=data$y,
        codes=data$codes,max.K=max(data$K),
        K=data$K,yearparty=data$yearparty,nyearparty=data$nyearparty)
}
jdata <- unrolly(jdata)

print("Running the model...")
system.time(
output <- MCMCordfactanal(jdata$y,
       	1,
	burnin=adapt,
	mcmc=iterations,
	thin=thin,
	verbose=T,
	seed=150113,
	store.scores=T,
	drop.constantvars=T)
          )
save("output","iterations","thin","jdata",file="jagsoutFull2kOrd.RData")
